package com.it.gw.modules.station.activity

import android.annotation.SuppressLint
import android.widget.Toast
import com.it.common.utils.DateUtils
import com.it.common.utils.RvSpacesItemUtils
import com.it.common.utils.isNotEmpty
import com.it.data.network.rsp.QuaTypeCountVo
import com.it.gw.R
import com.it.gw.base.BaseActivity
import com.it.gw.databinding.ActivityQualtypeStatisticsBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.modules.station.adapter.QualTypeCountListAdapter


//问题类型统计
class QualTypeStatisticsActivity : BaseActivity<ActivityQualtypeStatisticsBinding>() {

    companion object {
        const val TAG = "QualTypeStatisticsActivity"
    }

    private val datas by lazy { mutableListOf<QuaTypeCountVo>() }
    private val adapter by lazy {
        QualTypeCountListAdapter(context = this, datas = datas, listener = {

        })
    }

    override fun initObserver() {

        gqStationViewModel.quaTypeCountListLiveData.observe(this) {
            ProgressDialog.hide()
            binding.swipeRefreshLayout.isRefreshing = false
            datas.clear()
            if (it.result && it.data.isNotEmpty()) {
                it.data?.let { it1 ->
                    datas.addAll(it1)
                    // 统计 count 的累加值
                    val totalCount = it1.sumOf { it.count }
                    datas.forEach { d ->
                        d.totalCount = totalCount
                    }

                }

            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_LONG).show()
            }
            adapter.notifyDataSetChanged()

        }

    }


    @SuppressLint("RestrictedApi")
    override fun initListener() {
        binding.swipeRefreshLayout.setOnRefreshListener {
            getData()
        }
        binding.startTime.setOnClickListener {
            showDatePickerDialog {
                binding.startTime.text = it
            }
        }
        binding.endTime.setOnClickListener {
            showDatePickerDialog {
                binding.endTime.text = it
            }
        }
        binding.btnOk.setOnClickListener {
            ProgressDialog.show(this)
            getData()
        }

    }


    override fun initData() {
        ProgressDialog.show(this)
        getData()

    }


    private fun getData() {
        gqStationViewModel.quaTypeCount(
            startTime = binding.startTime.text.toString(),
            endTime = binding.endTime.text.toString()
        )
    }

    override fun initView() {
        setTitle(
            title = R.string.str_qual_static,
            isBack = true,
        )
        val map = hashMapOf(RvSpacesItemUtils.BOTTOM_DECORATION to 1)
        binding.rvList.addItemDecoration(RvSpacesItemUtils(map))
        binding.rvList.adapter = adapter
        binding.startTime.text = DateUtils.getFirstDayOfYear()
        binding.endTime.text = DateUtils.getToday()


    }


    override fun setContentView() {
        binding = ActivityQualtypeStatisticsBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}